package com.itheima.CaseAtm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * @ClassName ATM
 * @Author 16799
 * @Date 2025/7/1 16:10
 * @Version 1.0
 * @Description TODO
 *
 * 在包itheima下面的ATM案例，自己敲一遍
 **/
public class ATM {

    private ArrayList<Account> accounts = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    private Account loginAccount;

    public void start(){
        while (true){
            System.out.println("======ATM系统========");
            System.out.println("按1：用户登录");
            System.out.println("按2：用户开户");
            System.out.println("请选择");
            int command = sc.nextInt();

            switch (command){
                case 1:
                    login();
                    break;
                case 2:
                    createAccount();
                    break;
                default:
                    System.out.println("请重新输入1或2指令");
            }
        }
    }

    private void createAccount(){
        System.out.println("====用户开户=====");
        Account acc = new Account();

        System.out.println("请输入账户名称");
        acc.setUserName(sc.next());

        while (true){
            System.out.println("请输入性别");
            char sex = sc.next().charAt(0);
            if (sex == '男' || sex == '女'){
                acc.setSex(sex);
                break;
            }else {
                System.out.println("输入错误，只能输入男或女");
            }
        }

        while (true){
            System.out.println("请设置账户密码");
            String passWord = sc.next();
            System.out.println("请再次输入密码确认");
            String okPassWord = sc.next();

            if (okPassWord.equals(passWord)){
                acc.setPassWord(okPassWord);
                break;
            }else {
                System.out.println("两次输入密码不一致，请重新输入");
            }
        }

        System.out.println("请确认账户取现额度");
        double limit = sc.nextDouble();
        acc.setLimit(limit);

        String newCardId = createCardId();
        acc.setCardId(newCardId);

        accounts.add(acc);
        System.out.println("恭喜你"+acc.getUserName()+"开户成功,您的卡号是："+acc.getCardId());
    }

    private String  createCardId(){
        String cardId = "";
        Random r = new Random();
        while (true){
            for (int i = 0; i < 8; i++) {
                int data = r.nextInt(10);
                cardId+=data;
            }

            Account acc = getAccountByCardId(cardId);
            if (acc == null){
                return cardId;
            }
        }
    }

    private Account getAccountByCardId(String cardId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)){
                return acc;
            }
        }
        return null;
    }

    private void login() {
        System.out.println("=====用户登录========");
        if (accounts.size()==0){
            System.out.println("当前系统中无账户记录，请先开户");
            return;
        }

        while (true){
            System.out.println("请输入登录账户卡号");
            String  cardId = sc.next();

            Account acc = getAccountByCardId(cardId);
            if (acc == null){
                System.out.println("输入的卡号不存在，请重新输入");
            }else {
                while (true){
                    System.out.println("请输入密码");
                    String passWord = sc.next();

                    if (acc.getPassWord().equals(passWord)){
                        loginAccount = acc;
                        System.out.println("恭喜"+loginAccount.getUserName()+"成功登录系统，卡号为："+loginAccount.getCardId());

                        showUserCommand();
                        return;
                    }else {
                        System.out.println("输入的密码错误，请重新输入");
                    }
                }
            }
        }
    }

    private void showUserCommand(){
        while (true){
            System.out.println(loginAccount.getUserName() + "您可以选择如下功能进行账户的处理====");
            System.out.println("1、查询账户");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、密码修改");
            System.out.println("6、退出");
            System.out.println("7、注销当前账户");
            System.out.println("请选择：");

            int command = sc.nextInt();
            switch (command){
                case 1:
                    showLoginAccount();
                    break;
                case 2:
                    depositMoney();
                    break;
                case 3:
                    drawMoey();
                    break;
                case 4:
                    transferMoney();
                    break;
                case 5:
                    updatePassWord();
                    return;
                case 6:
                    System.out.println(loginAccount.getUserName()+"成功退出系统");
                    return;
                case 7:
                    if (deleteAccount()){
                        return;
                    }
                    break;
                default:
                    System.out.println("当前输入的操作序列不存在，重新输入");
            }
        }
    }

    private void showLoginAccount() {
        System.out.println("==当前您的账户信息如下：==");
        System.out.println("卡号：" + loginAccount.getCardId());
        System.out.println("户主：" + loginAccount.getUserName());
        System.out.println("性别：" + loginAccount.getSex());
        System.out.println("余额：" + loginAccount.getMoney());
        System.out.println("每次取现额度：" + loginAccount.getLimit());
    }

    private void depositMoney(){
        System.out.println("==存钱操作==");
        System.out.println("请您输入存款金额：");
        double money = sc.nextDouble();

        loginAccount.setMoney(loginAccount.getMoney()+money);
        System.out.println("恭喜你存入"+money+"元，存钱后的余额为"+loginAccount.getMoney());
    }

    private void drawMoey(){
        System.out.println("=====取钱操作=====");
        if (loginAccount.getMoney()<100){
            System.out.println("账户余额不足100，取不出");
            return;
        }

        while (true){
            System.out.println("请输入取款金额");
            double money = sc.nextDouble();

            if (loginAccount.getMoney()>=money){
                if (money>loginAccount.getLimit()){
                    System.out.println("单次取款金额超过每日限额"+loginAccount.getLimit());
                }else {
                    loginAccount.setMoney(loginAccount.getMoney()-money);
                    System.out.println("取款"+money+"成功,账户余额为"+loginAccount.getMoney());
                    break;
                }
            }else {
                System.out.println("账户余额不足，当前账户余额为："+loginAccount.getMoney());
            }
        }
    }

    private void transferMoney(){
        System.out.println(loginAccount.getUserName()+"账户进行转账操作");
        if (accounts.size()<2){
            System.out.println("当前系统仅有一个账户，无法转账");
            return;
        }

        if (loginAccount.getMoney() == 0){
            System.out.println("当前账户余额为零");
            return;
        }

        while (true){
            System.out.println("请输入转账对象的卡号");
            String cardId = sc.next();

            Account acc = getAccountByCardId(cardId);
            if (acc == null){
                System.out.println("输入的卡号账户不存在");
            }else {
                String  name = "*" + acc.getUserName().substring(1);
                System.out.println("请输入"+name+"的姓:");
                String preName = sc.next();
                if (acc.getUserName().startsWith(preName)) {
                    while (true){
                        System.out.println("请输入需要转账金额");
                        double money = sc.nextDouble();
                        if (loginAccount.getMoney()>money){
                            loginAccount.setMoney(loginAccount.getMoney()-money);
                            acc.setMoney(acc.getMoney()+money);
                            System.out.println("恭喜你转账成功");
                            return;
                        }else {
                            System.out.println("余额不足，无法转账，最多可转："+loginAccount.getMoney());
                        }
                    }
                }else {
                    System.out.println("对不起，当前输入卡号的账户姓名有问题");
                }
            }
        }
    }

    private boolean deleteAccount(){
        System.out.println("销户操作界面");
        System.out.println("是否确认需要进行销户：y/n");
        String command = sc.next();
        switch (command){
            case "y":
                if (loginAccount.getMoney()==0){
                    accounts.remove(loginAccount);
                    System.out.println("销户成功");
                    return true;
                }else {
                    System.out.println("当前账户有余额，不能销户");
                    return false;
                }
            default:
                System.out.println("不进行销户");
                return false;
        }
    }

    private void updatePassWord(){
        System.out.println("进入账户密码修改操作页面");
        while (true){
            System.out.println("输入当前账户的密码");
            String passWord = sc.next();

            if (loginAccount.getPassWord().equals(passWord)){
                while (true){
                    System.out.println("输入新密码");
                    String newPassWord = sc.next();
                    System.out.println("再次输入新密码");
                    String okPassWord = sc.next();

                    if (okPassWord.equals(newPassWord)){
                        loginAccount.setPassWord(okPassWord);
                        System.out.println("密码修改成功");
                        return;
                    }else {
                        System.out.println("前后两次输入的密码不正确");
                    }
                }
            }else {
                System.out.println("当前账户密码错误");
            }
        }
    }

}
